package com.egg.ylt.widget;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.OverScroller;
import android.widget.ScrollView;

import com.egg.ylt.widget.NewsScrollview;
import com.transitionseverywhere.utils.ReflectionUtils;
import com.yonyou.framework.library.common.log.Elog;
import com.yonyou.framework.library.common.utils.ReflectUtil;

import java.lang.reflect.Field;

/**
 * Created by lijiateng on 2019/2/25.
 */

public class MyScrollView extends NewsScrollview {
    /**
     * 覆盖默认的 scroller，方便调用 isFinished 方法
     */
    private OverScroller mScroller;
    /**
     * 判断是否按下的标记位
     */
    private boolean mInTouch = false;
    public MyScrollView(Context context) {
        super(context);
        setMyScroller(context);
    }
    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller(context);
    }
    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setMyScroller(context);
    }
    private void setMyScroller(Context context) {
        mScroller = new OverScroller(context);
        try {
            setField("android.widget.ScrollView","mScroller", mScroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 检测滚动是否停止的方法
     */
    private void checkScrollStop() {
        boolean mIsBeingDragged = false;
        try {
            mIsBeingDragged = (Boolean)getField("android.widget.ScrollView", "mIsBeingDragged");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(mScroller.isFinished() && !mIsBeingDragged && !mInTouch) {
            // scroll is stoped
            // TODO do something
            if (mListener != null) {
                mListener.onScrollStopped();
            }
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean result = super.onTouchEvent(ev);
        final int actionMasked = ev.getActionMasked();
        switch (actionMasked) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                mInTouch = true;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                mInTouch = false;
                checkScrollStop();
                break;
            default:
                break;
        }
        return result;
    }
    @Override
    public void computeScroll() {
        super.computeScroll();
        checkScrollStop();
    }

    private Object getField(Object target, String name) throws Exception {
        Field field = this.getClass().getDeclaredField(name);
        field.setAccessible(true);
        return field.get(this);
    }

    private void setField(Object target, String name, Object value) throws Exception {
        Field field = this.getClass().getDeclaredField(name);
        field.setAccessible(true);
        field.set(this, value);
    }

    public interface OnScrollStoppedListener {
        void onScrollStopped();
    }

    private OnScrollStoppedListener mListener;

    public void setOnScrollStoppedListener(OnScrollStoppedListener listener) {
        this.mListener= listener;
    }

}